package com.erp.framework.util.logger;

import java.sql.Connection;
import java.util.ArrayList;

import javax.transaction.UserTransaction;

import com.erp.framework.exceptions.ModifyRecordException;
import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.model.ejb.SimpleSearchMaintStyleEjb;
import com.erp.framework.util.user.User;

public class LoggerEjb extends SimpleSearchMaintStyleEjb {
    public void setEJBLogLevel() {
        Logger.setLogLevelFromSystem();
    }

    public void setEJBLogTimestamp() {
        Logger.setLogTimestampFromSystem();
    }

    public void setEJBLogToFile() {
        Logger.setLogToFileFromSystem();
    }

    public void updateTable(String functionName, String operation, ArrayList models, User user) throws ModifyRecordException {

        System.out.println("LoggerEJB: super_class=" + this.getClass().getSuperclass().getName());
        Connection conn = null;
        try {
            // ServiceLocator srvLocator = new ServiceLocator();
            ServiceLocator srvLocator = ServiceLocator.getInstance();
            UserTransaction userTrans = srvLocator.getUserTransaction();
            System.out.println("LoggerEJB.updateTable(), functionName=" + functionName);
            System.out.println("LoggerEJB.updateTable(), operation=" + operation);
            System.out.println("LoggerEJB.updateTable(), size of Models=" + models.size());

            try {
                userTrans.begin();
                conn = srvLocator.getDSDBConnection();
                /*
                 * for (int i = 0; i < models.size(); i++) { if
                 * (Constants.FUNCTION_SC.equalsIgnoreCase(functionName)) {
                 * GenericVO gvo = (GenericVO) models.get(i); GenericDAO dao =
                 * (GenericDAO) Class.forName(gvo.getDaoName()).newInstance();
                 * dao.updateTable(functionName, operation, models, user, conn); } }
                 */
                userTrans.commit();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
                srvLocator.userTransactionRollBack(userTrans);
                throw e;
            }
        } catch (java.lang.reflect.UndeclaredThrowableException ue) {
            Logger.logExceptions(this, ue);
            throw new ModifyRecordException(ue.getMessage());
        } catch (javax.transaction.RollbackException traxe) {
            Logger.logExceptions(this, traxe);
            throw new ModifyRecordException(traxe.getMessage());
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            throw new ModifyRecordException(e.getMessage());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }
    }

}
